// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

import axios from 'axios'

import VueClipboard from 'vue-clipboard2'
Vue.use(VueClipboard)
Vue.use(ElementUI)


//导航守卫
// 请求拦截
axios.interceptors.request.use(

  config => {
  // 判断是否存在token，如果存在的话，则每个http header都加上token
    let token = localStorage.getItem("token")
    // console.log(token)
    // console.log(!config.headers.hasOwnProperty('Authorization') )
    if (!config.headers.hasOwnProperty('Authorization') && token) {
      config.headers = {
          'X-Access-Token': token
          }
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  });

//响应拦截  判断token失效跳转
axios.interceptors.response.use(response =>{
  // console.log(response.status)
  if(response.status == '500'){
    localStorage.removeItem('TOKEN')
    this.$router.push('/login')
  }
  return response;
},error =>{
  alert("登录失效，请重新登录！")
  router.push('/login')
  return Promise.reject(error)//返回接口返回的错误信息
  
})

Vue.config.productionTip = false
Vue.prototype.$http = axios




new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
